﻿using Estsh.Core.Wms.IServices;
using Estsh.Core.Model.Result;
using Microsoft.AspNetCore.Mvc;
using Estsh.Core.Models;
using Estsh.Core.Controllers;

namespace Estsh.Core.Web.Areas.Wms.Controllers.Receiving
{
    [Route("wms/[controller]")]
    [ApiController]
    public class ReceiveController : WmsBaseController
    {
        private IReceiveService service;

        // GET: LoginController
        public ReceiveController(IReceiveService _service)
        {
            service = _service;
        }
        /// <summary>
        /// 正常收货
        /// </summary>
        /// <param name="cartonNo"></param>
        /// <param name="isRejection"></param>
        /// <param name="groupNo"></param>
        /// <returns></returns>
        [HttpPost("CheckCartonStatus")]
        public IActionResult CheckCartonStatus(Newtonsoft.Json.Linq.JObject jobj)
        {
            string cartonNo = jobj["cartonNo"].ToString();
            bool isRejection = Convert.ToBoolean(jobj["isRejection"].ToString());
            bool isQc = Convert.ToBoolean(jobj["isQc"].ToString());
            string groupNo = jobj["groupNo"].ToString();
            string loginId = jobj["loginId"].ToString();
            string supportingParts = jobj["supportingParts"].ToString();
            string cdNum = jobj["cdNum"].ToString();
            string orderNo = jobj["orderNo"].ToString();
			WmsResponseResult result = new WmsResponseResult();
            try
            {
                string obj = service.CheckCartonStatus(cartonNo, isRejection, groupNo, loginId, supportingParts,cdNum,orderNo, isQc);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 收货拒收，更改数据状态
        /// </summary>
        /// <param name="cartonNo"></param>
        /// <returns></returns>
        [HttpPost("UpdateCartonStatusInRejection")]
        public IActionResult UpdateCartonStatusInRejection(Newtonsoft.Json.Linq.JObject jobj)
        {
            string cartonNo = jobj["cartonNo"].ToString();
            string info = jobj["info"].ToString();
            string loginId = jobj["loginId"].ToString();
            string supportingParts = jobj["supportingParts"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string obj = service.UpdateCartonStatusInRejection(cartonNo, info, loginId, supportingParts);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        [HttpPost("UpdateCartonStatusInRejectionBycd")]
        public IActionResult UpdateCartonStatusInRejectionBycd(Newtonsoft.Json.Linq.JObject jobj)
        {
            string cartonNo = jobj["cartonNo"].ToString();
            string info = jobj["info"].ToString();
            string loginId = jobj["loginId"].ToString();
            string supportingParts = jobj["supportingParts"].ToString();
            string cdNum = jobj["cdNum"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string obj = service.UpdateCartonStatusInRejectionBycd(cartonNo, info, loginId, supportingParts, cdNum);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 通过箱条码获取收货订单的明细信息
        /// </summary>
        /// <param name="cartonNo"></param>
        /// <param name="receving"></param>
        /// <returns></returns>
        [HttpPost("GetCartonSummaryInfo")]
        public IActionResult GetCartonSummaryInfo(Newtonsoft.Json.Linq.JObject jobj)
        {
            string cartonNo = jobj["cartonNo"].ToString();
            string receving = jobj["receving"].ToString();
            string loginId = jobj["loginId"].ToString();
            string supportingParts = jobj["supportingParts"].ToString();

            WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.GetCartonSummaryInfo(cartonNo, receving, supportingParts, loginId);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 通过箱条码获取收货订单的明细信息
        /// </summary>
        /// <param name="cartonNo"></param>
        /// <param name="receving"></param>
        /// <returns></returns>
        [HttpPost("GetRecevingListing")]
        public IActionResult GetRecevingListing(Newtonsoft.Json.Linq.JObject jobj)
        {
			string cartonNo = jobj["cartonNo"].ToString();
			WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.GetRecevingListing(cartonNo);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 通过箱条码获取收货订单的明细信息
        /// </summary>
        /// <param name="cartonNo"></param>
        /// <param name="receving"></param>
        /// <returns></returns>
        [HttpPost("CreateQcInfo")]
        public IActionResult CreateQcInfo(Newtonsoft.Json.Linq.JObject jobj)
        {
            string receving = jobj["receving"].ToString();
            string loginId = jobj["loginId"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string obj = service.CreateQcInfo(receving, loginId);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="cartonNo"></param>
        /// <param name="receving"></param>
        /// <returns></returns>
        [HttpPost("GetRejectionInfo")]
        public IActionResult GetRejectionInfo()
        {

            WmsResponseResult result = new WmsResponseResult();
            try
            {
                List<WmsRejection> obj = service.GetRejectionInfo();
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 自动提交
        /// </summary>
        /// <param name="jobj"></param>
        /// <returns></returns>
        [HttpPost("CheckStatus")]
        public IActionResult CheckStatus(Newtonsoft.Json.Linq.JObject jobj)
        {
            string receving = jobj["receving"].ToString();
			string loginId = jobj["loginId"].ToString();
			WmsResponseResult result = new WmsResponseResult();
            try
            {
                string obj = service.CheckStatus(receving, loginId);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

		/// <summary>
		/// 判断主表状态-自动提交
		/// </summary>
		/// <param name="jobj"></param>
		/// <returns></returns>
		[HttpPost("CheckPurchaseStatus")]
		public IActionResult CheckPurchaseStatus(Newtonsoft.Json.Linq.JObject jobj)
		{
			string receving = jobj["receving"].ToString();
			string loginId = jobj["loginId"].ToString();
			WmsResponseResult result = new WmsResponseResult();
			try
			{
				string obj = service.CheckPurchaseStatus(receving, loginId);
				result.Success = true;
				result.Data = obj;
			}
			catch (Exception ex)
			{
				result.Msg = ex.Message;
			}
			return Json(result);
		}

		/// <summary>
		/// 得到 标准收货列表
		/// </summary>
		/// <returns></returns>
		[HttpPost("GetStandardList")]
        public IActionResult GetStandardList(Newtonsoft.Json.Linq.JObject jobj)
        { 
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string cartonno = jobj["cartonno"].ToString();

                List<WmsPurchase> obj = service.GetStandardList(cartonno);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }
		/// <summary>
		/// 当前收货中的订单列表
		/// </summary>
		/// <param name="jobj"></param>
		/// <returns></returns>
		[HttpPost("GetRecevingListByOrderNo")]
        public IActionResult GetRecevingListByOrderNo(Newtonsoft.Json.Linq.JObject jobj)
        {
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string orderNo = jobj["orderNo"].ToString();
                string cartonNo = jobj["cartonNo"].ToString();
                SetObjectDetail obj = service.GetRecevingListByOrderNo(orderNo,cartonNo);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }
        
		/// <summary>
		/// 订单收货列表
		/// </summary>
		/// <param name="jobj"></param>
		/// <returns></returns>
		[HttpPost("GetRealOrderList")]
        public IActionResult GetRealOrderList(Newtonsoft.Json.Linq.JObject jobj)
        {
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string orderNo = jobj["orderNo"].ToString();

                SetObjectDetail obj = service.GetRealOrderList(orderNo);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }
		/// <summary>
		/// 根据登录人id查询默认库位
		/// </summary>
		/// <param name="jobj"></param>
		/// <returns></returns>
		[HttpPost("GetLogIdByLocateName")]
		public IActionResult GetLogIdByLocateName(Newtonsoft.Json.Linq.JObject jobj)
		{
			string loginId = jobj["loginId"].ToString();
			WmsResponseResult result = new WmsResponseResult();
			try
			{
			
                SetObjectDetail obj= service.GetLogIdByLocateName(loginId);
				result.Success = true;
                result.Data = obj;

			}
			catch (Exception ex)
			{
				result.Msg = ex.Message;
			}

			return Json(result);
		}


		/// <summary>
		/// 切换库位-验证库位
		/// </summary>
		/// <param name="jobj"></param>
		/// <returns></returns>
		[HttpPost("GetLocateByName")]
        public IActionResult GetLocateByName(Newtonsoft.Json.Linq.JObject jobj)
        {

			string locateName = jobj["locateName"].ToString().Trim();
			string loginId = jobj["loginId"].ToString();

			WmsResponseResult result = new WmsResponseResult();
			try
			{
				SetObjectDetail obj = service.GetLocateByName(locateName, loginId);
				result.Success = true;
				result.Data = obj;
			}
			catch (Exception ex)
			{
				result.Msg = ex.Message;
			}
			return Json(result);
		}

        /// <summary>
        /// 正常收货
        /// </summary>
        /// <param name="jobj"></param>
        /// <returns></returns>
        [HttpPost("New_CheckCartonStatus")]
        public IActionResult New_CheckCartonStatus(Newtonsoft.Json.Linq.JObject jobj)
        {
			string cartonNo = jobj["cartonNo"].ToString();//箱条码
			bool isRejection = Convert.ToBoolean(jobj["isRejection"].ToString());//isRejection是否拒收
			bool isQc = Convert.ToBoolean(jobj["isQc"].ToString());
			string groupNo = jobj["groupNo"].ToString(); //组托生成的UUID
			string loginId = jobj["loginId"].ToString();//登录人id
			string supportingParts = jobj["supportingParts"].ToString();//零件
			string cdNum = jobj["cdNum"].ToString();//
			string orderNo = jobj["orderNo"].ToString();//当前操作的收货订单号
            string locateName = jobj["locateName"].ToString();//当前库位
			string isGroup = jobj["isGroup"].ToString().Trim();//是否组托1：是 0：不是
			WmsResponseResult result = new WmsResponseResult();
			try
			{
				SetObjectDetail obj = service.New_CheckCartonStatus( cartonNo,isRejection,groupNo,loginId, supportingParts,cdNum,orderNo, locateName, isGroup);
				result.Success = true;
				result.Data = obj;
			}
			catch (Exception ex)
			{
				result.Msg = ex.Message;
			}
			return Json(result);
		}

		/// <summary>
		/// 修改实际库存数量
		/// </summary>
		/// <param name="jobj"></param>
		/// <returns></returns>
		[HttpPost("Up_StockQty")]
		public IActionResult Up_StockQty(Newtonsoft.Json.Linq.JObject jobj)
        {
			string cartonNo = jobj["cartonNo"].ToString();//箱条码
			string groupNo = jobj["groupNo"].ToString(); //组托生成的UUID
			string loginId = jobj["loginId"].ToString();//登录人id
			string orderNo = jobj["orderNo"].ToString();//当前操作的收货订单号
            string qtyReceived = jobj["qtyReceived"].ToString();//修改的数量
            string locateName= jobj["locateName"].ToString();//库位
			WmsResponseResult result = new WmsResponseResult();
			try
			{
				string obj = service.Up_StockQty(cartonNo, groupNo, loginId, orderNo, qtyReceived, locateName);
				result.Success = true;
				result.Data = obj;
			}
			catch (Exception ex)
			{
				result.Msg = ex.Message;
			}
			return Json(result);
		}


		/// <summary>
		/// 通过箱条码获取收货订单的明细信息（后绑定新增方法）
		/// </summary>
		/// <param name="cartonNo"></param>
		/// <param name="receving"></param>
		/// <returns></returns>
		[HttpPost("GetCartonSummaryInfoNew")]
		public IActionResult GetCartonSummaryInfoNew(Newtonsoft.Json.Linq.JObject jobj)
		{
			string cartonNo = jobj["cartonNo"].ToString();
			string receving = jobj["receving"].ToString();
			string loginId = jobj["loginId"].ToString();
			string supportingParts = jobj["supportingParts"].ToString();
            string locateName = jobj["locateName"].ToString();

			WmsResponseResult result = new WmsResponseResult();
			try
			{
				SetObjectDetail obj = service.GetCartonSummaryInfoNew(cartonNo, receving, supportingParts, loginId, locateName);
				result.Success = true;
				result.Data = obj;
			}
			catch (Exception ex)
			{
				result.Msg = ex.Message;
			}
			return Json(result);
		}

		/// <summary>
		/// 通过订单号获取收货订单的明细信息（后绑定新增方法）
		/// </summary>
		/// <param name="cartonNo"></param>
		/// <param name="receving"></param>
		/// <returns></returns>
		[HttpPost("GetPurchaseSummaryInfo")]
		public IActionResult GetPurchaseSummaryInfo(Newtonsoft.Json.Linq.JObject jobj)
		{
			string cartonNo = jobj["cartonNo"].ToString();
			string receving = jobj["receving"].ToString();
			string loginId = jobj["loginId"].ToString();
			string supportingParts = jobj["supportingParts"].ToString();
            string nstock = jobj["nstock"].ToString();
			WmsResponseResult result = new WmsResponseResult();
			try
			{
				SetObjectDetail obj = service.GetPurchaseSummaryInfo(cartonNo, receving, supportingParts, loginId, nstock);
				result.Success = true;
				result.Data = obj;
			}
			catch (Exception ex)
			{
				result.Msg = ex.Message;
			}
			return Json(result);
		}





	}
}
